package com.ym.b2b.salesman.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.ym.b2b.commons.exception.BaseException;
import com.ym.b2b.commons.service.BaseService;
import com.ym.b2b.salesman.dao.EmBSalesmanDAO;
import com.ym.b2b.salesman.service.EmBSalesmanService;
import com.ym.b2b.salesman.sqlbean.EmBSalesman;
import com.ym.b2b.user.dao.EmCUserDAO;
import com.ym.b2b.user.sqlbean.EmCUser;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;

/**
 * @ClassName: EmBSalesmanServiceImpl
 * @Description: TODO
 * @Author tangxj
 * @Date 2015-3-18 上午10:55:57
 * @Modify
 * @CopyRight B2B小组
 */

@Service
public class EmBSalesmanServiceImpl extends BaseService implements EmBSalesmanService {

    private Logger         logger = Logger.getLogger(getClass());
    @Autowired
    private EmBSalesmanDAO emBSalesmanDAO;
    @Autowired
    private EmCUserDAO     userdao;

    private Logger         dbLog = Logger.getLogger("db");
    
    @Override
    public Result searchPageEmBSalesman(Page<EmBSalesman> page, EmBSalesman emBSalesman) throws DataAccessException {
        Result result = new Result();

        try {

            result.setResultPage(emBSalesmanDAO.searchPageEmBSalesman(emBSalesman, page));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "查询异常");
        }
        return result;
    }

    @Override
    public Result showEmBSalesmanDetail(EmBSalesman emBSalesman) throws DataAccessException {
        Result result = new Result();
        try {
            // 业务员详情
            emBSalesman = emBSalesmanDAO.showEmBSalesmanDetail(emBSalesman);

            // 已添加用户
            EmCUser emCUser = new EmCUser();
            emCUser.setExtendCode(emBSalesman.getSalesmanNo());
            emCUser.setUserType(3);
            List<EmCUser> userList = userdao.searchEmCUserList(emCUser);
            result.putData("emBSalesman", emBSalesman);
            result.setRows(userList);
            result.setResult("获取业务员详情成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "获取业务员详情失败!");
        }

        return result;
    }

    @Override
    public Result insertEmBSalesman(EmBSalesman emBSalesman) throws DataAccessException {
        Result result = checkInputEmBSalesman(emBSalesman);
        if (!result.getCode().equals("200")) return result;
        try {
            emBSalesmanDAO.insertEmBSalesman(emBSalesman);
            dbLog.info("INSERT-->EM_B_SALESMAN 业务员ID：" + emBSalesman.getSalesmanId() +"  各字段值："+JSON.toJSONString(emBSalesman));
            result.setResult("添加业务员成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "添加业务员失败！");

        }
        return result;
    }

    @Override
    public Result updateEmBSalesman(EmBSalesman emBSalesman) throws DataAccessException {
        Result result = checkInputEmBSalesman(emBSalesman);
        if (!result.getCode().equals("200")) return result;
        try {
            emBSalesmanDAO.updateEmBSalesman(emBSalesman);
            if (emBSalesman.getSalesmanType() == 1) {
                emBSalesmanDAO.updateEmBSalesmanForType1(emBSalesman);
            } else {
                emBSalesmanDAO.updateEmBSalesmanForType2(emBSalesman);
            }
            dbLog.info("UPDATE-->EM_B_SALESMAN 业务员ID：" + emBSalesman.getSalesmanId() +"  各字段值："+JSON.toJSONString(emBSalesman));
            result.setResult("修改业务员成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "修改业务员失败！");

        }
        return result;
    }

    @Override
    public Result deleteEmBSalesman(EmBSalesman emBSalesman) throws DataAccessException {
        Result result = new Result();
        try {
            emBSalesmanDAO.deleteEmBSalesman(emBSalesman);
            dbLog.info("DELETE-->EM_B_SALESMAN 业务员ID：" + emBSalesman.getSalesmanId());
            result.setResult("删除业务员成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "删除业务员失败！");

        }
        return result;
    }

    @Override
    public Result deleteMultipleEmBSalesmans(String salesmanIdList) throws DataAccessException {
        Result result = new Result();
        try {
            emBSalesmanDAO.deleteMultipleEmBSalesmans(salesmanIdList);
            dbLog.info("DELETE(Multiple删除)-->EM_B_SALESMAN 业务员ID：" + salesmanIdList);
            result.setResult("删除业务员成功!");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new BaseException(Result.CODE_100, "删除业务员失败！");

        }
        return result;
    }

    @Override
    public Result editMultipleEmBSalesmansStatus(String EmBSalesmanIdList) throws DataAccessException {
        // TODO Auto-generated method stub
        return null;
    }

    /**
     * 输入参数检查
     * 
     * @Title: checkInputEmBSalesman
     */
    private Result checkInputEmBSalesman(EmBSalesman p) {
        Result result = new Result();
        if (StringUtils.isBlank(p.getSalesmanName())) {
            result.setCodeResult(Result.CODE_100, "缺少salesmanName姓名!");
            return result;
        }
        if (StringUtils.isBlank(p.getMobile())) {
            result.setCodeResult(Result.CODE_100, "缺少mobile手机!");
            return result;
        }
        if (StringUtils.isBlank(p.getPhone())) {
            result.setCodeResult(Result.CODE_100, "缺少phone电话!");
            return result;
        }
        if (p.getSalesmanType() == 1) {
            if (StringUtils.isBlank(p.getProviderCode()) || StringUtils.isBlank(p.getProviderName())) {
                result.setCodeResult(Result.CODE_100, "请筛选供货商!");
                return result;
            }
        } else if (p.getSalesmanType() == 2) {
            if (p.getIsAgentProvider() == null) {
                result.setCodeResult(Result.CODE_100, "请选择是否代理供货商!");
                return result;
            }
            if (p.getAddBuyer() == null) {
                result.setCodeResult(Result.CODE_100, "请选择身份代理权限!");
                return result;
            }

        }
        return result;

    }
}
